package com.guisu.animation.loading.three_circle_loading

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.RelativeLayout
import androidx.core.animation.addListener

class LoadingView: RelativeLayout {

    private var mLeftView: CircleView? = null
    private var mMiddleView: CircleView? = null
    private var mRightView: CircleView? = null

    private var mTranslationDistance = 50
    private val ANIMATION_TIME = 250L

    private var mIsStopAnimator = false
    constructor(context: Context): this(context, null)
    constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) {
        mTranslationDistance = dip2px(mTranslationDistance)
        setBackgroundColor(Color.WHITE)
        // 添加三个圆形
        mLeftView = getCircleView(context)
        mLeftView?.exchangeColor(Color.parseColor("#ffa467"))
        mMiddleView = getCircleView(context)
        mMiddleView?.exchangeColor(Color.parseColor("#6ec2ff"))
        mRightView = getCircleView(context)
        mRightView?.exchangeColor(Color.parseColor("#72d966"))

        addView(mLeftView)
        addView(mRightView)
        addView(mMiddleView)

        post {
            // 让布局实例化好之后再去开启动画
            expendAnimation()
        }
    }

    /**
     * 展开动画
     */
    private fun expendAnimation() {
        if (mIsStopAnimator) {
            return
        }

        // 左边跑
        val leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView!!, "translationX", 0f, -mTranslationDistance.toFloat())
        // 右边跑
        val rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView!!, "translationX", 0f, mTranslationDistance.toFloat())

        // 弹性效果  荡秋千一样  差值器  刚开始快越来越慢

        AnimatorSet().apply {
            duration = ANIMATION_TIME
            interpolator = DecelerateInterpolator()
            playTogether(leftTranslationAnimator, rightTranslationAnimator)
            addListener(onEnd = {
                // 往里面跑
                innerAnimation()
            })
            start()
        }
    }

    private fun innerAnimation() {
        if (mIsStopAnimator) {
            return
        }

        // 左边跑
        val leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView!!, "translationX", -mTranslationDistance.toFloat(), 0f)
        // 右边跑
        val rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView!!, "translationX", mTranslationDistance.toFloat(), 0f)

        AnimatorSet().apply {
            duration = ANIMATION_TIME
            interpolator = AccelerateInterpolator()
            playTogether(leftTranslationAnimator, rightTranslationAnimator)
            addListener(onEnd = {
                // 切换颜色顺序  左边给中间  中间给右边  右边给左边
                val leftColor = mLeftView!!.getColor()
                val rightColor = mRightView!!.getColor()
                val middleColor = mMiddleView!!.getColor()

                mMiddleView!!.exchangeColor(leftColor)
                mRightView!!.exchangeColor(middleColor)
                mLeftView!!.exchangeColor(rightColor)

                // 往外面跑
                expendAnimation()
            })
            start()
        }
    }

    private fun getCircleView(context: Context): CircleView {
        return CircleView(context).apply {
            val params = LayoutParams(dip2px(20), dip2px(20)).apply {
                addRule(CENTER_IN_PARENT)
            }
            layoutParams = params
        }
    }

    private fun dip2px(dip: Int): Int {
        return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip.toFloat(), resources.displayMetrics)).toInt()
    }

    override fun setVisibility(visibility: Int) {
        super.setVisibility(View.INVISIBLE)
        // 清理动画
        mLeftView!!.clearAnimation()
        mMiddleView!!.clearAnimation()
        mRightView!!.clearAnimation()

        // 把loadingView从父布局移除
        val parent = this.parent as ViewGroup
        parent.removeView(this)
        removeAllViews()

        mIsStopAnimator = true
    }
}